/***************************************************************************************
Firm-embedded productivity and cross-country income differences
Alviarez, Cravino and Ramondo
Journal of Political Economy (2022)

Program: table_A2_elasticity.do
Date: October 2022

Description: Reproduces Table A2: Estimates of sectoral elasticities, \beta_{j}.

*****************************************************************************************/

*-------------------------------------------------------------------------------
global typeden=1
include "set_directories.do"
set memory 64g
global lf "LF"

*Log
cap log close
log using "${clogs}/table_A2_elasticity.log",replace
*-------------------------------------------------------------------------------




************************ TABLE A2  **************************
local mastervar "_sales"
local grvar0 "LP_ppp_emp_pwt" 
global ctryrel FR
local yy "2016"
local nn=10 
*minimum number of sectors allowed per year-isocode*
local sample `" isocode=="" "'



*Restricting the set of isocode-years for those pairs with less than `nn' sectors
*----------------------------------------------------------
clear all
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear

gsort year type sector -DA
by year isocode, sort: egen num=count( fe_location )
table isocode year, c(mean  num)
keep if num>=`nn'
keep year isocode
duplicates drop 
tempfile iso
save `iso', replace 


*Restricting the set of isocode-years for those pairs with less than `nn' sectors
*----------------------------------------------------------
use "${data}/estimates_sec_naics_sales_s1_base_woparent.dta", clear
keep if year==`yy'
drop if sector1==sector
tab type
replace DA=. if num_aff<3
replace DP=. if num_aff<3
levelsof sector, local(seclist)

merge m:1 year isocode using `iso'
keep if _merge==3
drop _merge
capture rename DA D_A
capture rename DP D_P
drop if D_A==. | (D_A==0 & isocode!="FR")
drop if `sample'  
display "`drop if `sample''"


*Bring some aggregate control variables
*----------------------------------------------------------
merge m:1 year isocode sector using "${data}/aggregates_tfp_lp_klems.dta"
drop if _merge==2
drop _merge
gen k_y=ln_ck-ln_rgdpo

levelsof sector, local(secs)
gen one=1
tempfile temp
save `temp', replace 	

*----------------------------------------------
use `temp', clear
keep year type sector1 sector
duplicates drop 
sort sector1 sector

gen coef_nocontrols=.
gen se_nocontrols=.
gen coef_cap_hc=.
gen se_cap_hc=.

tempfile sectorelasticity
save `sectorelasticity', replace 


*----------------------------------------------------------
local rr "" 
local var_dep "ln_`grvar0'"
local var D_A
local abslist "one"


*Creating Table A2: 
*----------------------------------------------------------
foreach cover of local secs  {
display "`cover'"

*----------------------------------
clear all
use `temp', clear
keep if sector=="`cover'"

*Baseline regression, no controls (per sector)
*----------------------------------------
reghdfe `var_dep' `var', abs(`abslist')  
mat b = e(b)
mat V = e(V)
global beta1: display %-03.2fc round(b[1,1],0.01)
global se_beta1: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$beta1, $se_beta1"


*Controlling for K/Y and HC (per sector) 
*----------------------------------------
reghdfe `var_dep' `var' k_y ln_hc, abs(`abslist') 
mat b = e(b)
mat V = e(V)
global beta2: display %-03.2fc round(b[1,1],0.01)
global se_beta2: display %-03.2fc round(sqrt(V[1,1]),0.01)
display "$beta2, $se_beta2"

use `sectorelasticity', clear
replace coef_nocontrols=$beta1 if sector=="`cover'"
replace se_nocontrols=$se_beta1 if sector=="`cover'"
replace coef_cap_hc=$beta2 if sector=="`cover'"
replace se_cap_hc=$se_beta2 if sector=="`cover'"
save, replace
}
*

clear all
use  `sectorelasticity', clear
*Export table to excel
export delimited using "${rappendix}/table_A2_elasticity.csv", datafmt replace

log close

